package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Activity;
import com.quickcancellation.crm.pojo.entity.Userinfo;
import com.quickcancellation.crm.pojo.vo.RoleVo;
import com.quickcancellation.crm.service.IActivityService;
import com.quickcancellation.crm.service.IRoleService;
import com.quickcancellation.crm.service.IUserinfoService;
import com.quickcancellation.crm.utils.DateUtils;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行动计划主表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/activity")
@CrossOrigin
@Api(tags = "行动计划管理")
@SaCheckLogin
@SaCheckRole("activity")
public class ActivityController {
    @Resource
    private IActivityService activityService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private IUserinfoService userinfoService;

    @Resource
    private IRoleService roleService;


    @PostMapping("/list")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getActivityList(@RequestBody PageParamsDto<Map> params) {

        PageDto<Map> pageDto = PageUtils.getPageParams(params);


        List<Map> resultList = activityService.getActivityList(pageDto);

        Long count = activityService.getActivityListCount(pageDto);

        return ResponseUtil.success(count, resultList);
    }




    @PostMapping("/getActivity")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getActivity(@RequestBody Map params) {

        Long activityId = MapUtils.getLong(params, "activityId");

        Activity activity = activityService.getById(activityId);

        return ResponseUtil.success(1,activity);
    }


    @PostMapping("/save")
    @ApiOperation("保存")
    @LogAspect
    public ResponseDto save(@RequestBody Activity activity) {

        if (activityService.list(new QueryWrapper<Activity>().eq("activityName", activity.getActivityName())).size() > 0) {
            return ResponseUtil.fail("活动名称已经存在");
        }


        if (activityService.list(new QueryWrapper<Activity>().eq("activityCode", activity.getActivityCode())).size() > 0) {
            return ResponseUtil.fail("活动编码已经存在");
        }

        if (activityService.list(new QueryWrapper<Activity>().eq("activityName", activity.getActivityName())).size() > 0) {
            return ResponseUtil.fail("活动名称已经存在");
        }



        // 待审核
        activity.setAuditStatus(0);

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);

        String userId = userInfo.getId();

        activity.setUpdateTime(DateUtils.getNow());
        activity.setCreateTime(DateUtils.getNow());
        activity.setCreateOp(userId);
        activity.setUpdateOp(userId);

//        activityService.save(activity);


        boolean result = activityService.save(activity);

        return ResponseUtil.ok(result);
    }


    @PostMapping("/audit")
    @ApiOperation("审核")
    @LogAspect
    public ResponseDto audit(@RequestBody Map<String,Object> params) {

        boolean checkRole = false;

        Long activityId = MapUtils.getLong(params, "activityId");
        Integer auditStatus = MapUtils.getInteger(params, "auditStatus");
        String auditRemark = MapUtils.getString(params, "auditRemark");

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);
        String userId = userInfo.getId();

        List<RoleVo> roleList = roleService.getRoleVoByUsernumber(userInfo.getUsernumber());

        // 校验用户是否有权限执行这个操作
        for (RoleVo roleVo : roleList) {
            if (roleVo.getRoleName().equals("业务经理")){
                checkRole = roleVo.isCheck();
                break;
            }
        }

        if (!checkRole){
            return ResponseUtil.fail("您无权限进行审核");
        }

        if (null == auditStatus){
            return ResponseUtil.fail("请选择审核状态");
        }

        if ((1 != auditStatus) && (2 != auditStatus)){
            return ResponseUtil.fail("请选择正确的审核状态");
        }


        Activity activity = new Activity();
        activity.setId(activityId);
        activity.setAuditStatus(auditStatus);
        activity.setAuditRemark(auditRemark);
        activity.setAuditOp(userId);
        activity.setAuditTime(DateUtils.getNow());


        boolean result = activityService.updateById(activity);

        return ResponseUtil.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    @LogAspect
    public ResponseDto delete(@RequestBody Map<String,Object> params) {

        Long activityId = MapUtils.getLong(params, "activityId");

        boolean res = activityService.removeById(activityId);

        return ResponseUtil.ok(res);
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    @LogAspect
    public ResponseDto update(@RequestBody Map params) {

        String classGuid = MapUtils.getString(params, "classGuid");
        String saleAreaId = MapUtils.getString(params, "saleAreaId");

        String activityType = MapUtils.getString(params, "activityType");
        Long id = MapUtils.getLong(params, "id");

        Activity activity = new Activity();
        activity.setId(id);
        activity.setSaleAreaId(Long.valueOf(saleAreaId));
        activity.setClassGuid(classGuid);
        activity.setClassGuid(activityType);

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);

        String userId = userInfo.getId();

        activity.setUpdateOp(userId);
        activity.setUpdateTime(DateUtils.getNow());

        boolean result = activityService.updateById(activity);


        return ResponseUtil.ok(result);
    }

    @PostMapping("/hasPermission")
    @ApiOperation("是否拥有权限")
    @LogAspect
    public ResponseDto hasPermission() {

        boolean checkRole = false;

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);
        List<RoleVo> roleList = roleService.getRoleVoByUsernumber(userInfo.getUsernumber());

        // 校验用户是否有权限执行这个操作
        for (RoleVo roleVo : roleList) {
            if (roleVo.getRoleName().equals("业务经理")){
                checkRole = roleVo.isCheck();
                break;
            }
        }

        if (!checkRole){
            return ResponseUtil.fail("您无权限进行审核");
        }
        return ResponseUtil.success(1,checkRole);
    }

}
